package com.song.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.util.PoitlIOUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.song.annotation.ServiceLog;
import com.song.entity.User;
import com.song.entity.UserLog;
import com.song.model.CarModel;
import com.song.model.ResultEntity;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Api(tags = "用户 控制器")
@RestController
public class UserController {
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/get")
    public Object get(){
        User user = new User();
        user.setXm("三儿");
        user.setJe(9007199254740991L);
        user.setCjsj(new Date());
        user.setCsrq(LocalDate.now());
        user.setCssj(LocalDateTime.now());
        return user;
    }

    @ServiceLog(value = UserLog.class, czlx = "1")
    @PostMapping("/add")
    public Object add(@RequestBody User user) {
        try {
            User jsonUser = objectMapper.readValue("{\"xm\":\"张三\",\"cjsj\":\"2023-09-10 09:09:09\",\"csrq\":\"2023-09-10\",\"cssj\":\"2023-09-10 09:09:09\"}", User.class);
            System.out.println(jsonUser.toString());
            List<User> users = objectMapper.readValue("[{\"xm\":\"张三\",\"cjsj\":\"2023-09-10 09:09:09\",\"csrq\":\"2023-09-10\",\"cssj\":\"2023-09-10 09:09:09\"}]", new TypeReference<ArrayList<User>>() {});
            String userStr = objectMapper.writeValueAsString(user);
            System.out.println(userStr);
            return users;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/exportWord")
    public void exportWord(HttpServletResponse response) throws IOException {
        List<CarModel> list = initDatas();
        Map<String, Object> map = new HashMap<>();
        map.put("carList", list);
        LoopRowTableRenderPolicy loopRowTableRenderPolicy = new LoopRowTableRenderPolicy();
        Configure configure = Configure.builder()
                .bind("carList", loopRowTableRenderPolicy)
                .build();
        XWPFTemplate xwpfTemplate = XWPFTemplate.compile("D:\\song\\gitee\\spring-boot-jackson\\src\\main\\resources\\word导出模板.docx", configure).render(map);
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("word导出模板.docx", "UTF-8"));
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        xwpfTemplate.write(bos);
        bos.flush();
        out.flush();
        PoitlIOUtils.closeQuietlyMulti(xwpfTemplate, bos, out);
    }

    public List<CarModel> initDatas(){
        List<CarModel> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            CarModel carModel = new CarModel();
            carModel.setCzxm("四哥");
            carModel.setCzzjhm("123456");
            carModel.setCzlxfs("13767678888");
            carModel.setPp("品牌"  + (i + 1));
            carModel.setXh("型号" + (i + 1));
            carModel.setZp("D:\\song\\gitee\\spring-boot-jackson\\src\\main\\resources\\cl" + (i + 1) + ".png");
            list.add(carModel);
        }
        return list;
    }

    @PostMapping("setSession")
    public ResultEntity setSession(HttpSession httpSession){
        System.out.println(httpSession.getId());
        return ResultEntity.success();
    }
}
